﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Medical.Business;

/// <summary>
/// 疾病页面
/// </summary>
public partial class Disease : WebPage
{
    protected int nSortID = 0;

    protected string strUrlName = string.Empty;

    protected int nCount = 0;
    protected string strAd = string.Empty;
    protected void Page_Load(object sender, EventArgs e)
    {
        InitConfig();
        if (!Page.IsPostBack)
        {
            InitData();   
        }
    }
    protected void Page_LoadComplete(object sender, EventArgs e)
    {
        DataTable dtAd = Until.GetWebBanner();
        if (dtAd != null && dtAd.Rows.Count > 0)
        {
            DataRow[] drList = dtAd.Select("OrderNum=" + nSortID.ToString());
            if (drList.Length > 0)
            {
                Image imgHead = Master.FindControl("imgHead") as Image;
                imgHead.ImageUrl = "/" + WebConfigInfo.FilePlace + "d/" + drList[0]["ImgPlace"].ToString();
            }
        }
    }

    /// <summary>
    /// 数据初始化
    /// </summary>
    protected void InitConfig()
    {
        if (RouteData.Values["urlname"] != null)
        {
            strUrlName = RouteData.Values["urlname"].ToString();
            DataTable dtSort = new BisSortInfo().GetSortInfo(strUrlName);
            
            if (dtSort != null && dtSort.Rows.Count > 0)
            {
                nSortID = Convert.ToInt32(dtSort.Rows[0]["ID"]);
               
                string strPageTitle = string.IsNullOrEmpty(dtSort.Rows[0]["PageTitle"].ToString()) ? (dtSort.Rows[0]["Name"].ToString() + "_" + strWebTitle) : dtSort.Rows[0]["PageTitle"].ToString();
                SetPageInfo(strPageTitle, dtSort.Rows[0]["PageKeywords"].ToString(), dtSort.Rows[0]["PageDescription"].ToString());
            }
            else
            {
                Response.Redirect("/404.html");
            }
        }
        else
        {
            Response.Redirect("/404.html");
        }
    }

    /// <summary>
    /// 页面数据初化
    /// </summary>
    protected void InitData()
    {
        BisArticle bisArticle = new BisArticle();
        DataTable dtArticle = bisArticle.GetIsTopArticle(10, ArticelTypeEnum.Disease, nSortID);
        string strInfo = string.Empty;
        if (dtArticle.Rows.Count > 0)
        {
            ltlTitle.Text = "<a href=\"" + Until.GetUrlName(dtArticle.Rows[0]["PSortUrlName"], dtArticle.Rows[0]["SortUrlName"], dtArticle.Rows[0]["ID"]) + "\" target=\"_blank\" title=\"" + dtArticle.Rows[0]["Name"].ToString() + "\">" + Server.HtmlEncode(dtArticle.Rows[0]["Name"].ToString()) + "</a>";
            strInfo = Common.NoHTML(dtArticle.Rows[0]["Info"].ToString()).Trim();
            ltlInfo.Text = (strInfo.Length > 40 ? strInfo.Substring(0, 40) : strInfo) + "…" +
                             "<a href=\"" + Until.GetUrlName(dtArticle.Rows[0]["PSortUrlName"], dtArticle.Rows[0]["SortUrlName"], dtArticle.Rows[0]["ID"]) + "\" title=\"" + dtArticle.Rows[0]["Name"].ToString() + "\" target=\"_blank\">[详情]</a>";
            dtArticle.Rows.RemoveAt(0);
            dtArticle.AcceptChanges();
        }
        if (dtArticle.Rows.Count > 0)
        {
            ltlTitle1.Text = "<a href=\"" + Until.GetUrlName(dtArticle.Rows[0]["PSortUrlName"], dtArticle.Rows[0]["SortUrlName"], dtArticle.Rows[0]["ID"]) + "\" target=\"_blank\" title=\"" + dtArticle.Rows[0]["Name"].ToString() + "\">" + Server.HtmlEncode(dtArticle.Rows[0]["Name"].ToString()) + "</a>";
            strInfo = Common.NoHTML(dtArticle.Rows[0]["Info"].ToString()).Trim();
            ltlInfo1.Text = (strInfo.Length > 40 ? strInfo.Substring(0, 40) : strInfo) + "…" +
                             "<a href=\"" + Until.GetUrlName(dtArticle.Rows[0]["PSortUrlName"], dtArticle.Rows[0]["SortUrlName"], dtArticle.Rows[0]["ID"]) + "\" title=\"" + dtArticle.Rows[0]["Name"].ToString() + "\" target=\"_blank\">[详情]</a>";
            dtArticle.Rows.RemoveAt(0);
            dtArticle.AcceptChanges();
        }

        rptArticle.DataSource = dtArticle;
        rptArticle.DataBind();

        DataTable dtSort = new BisSortInfo().GetSortList(nSortID, ArticelTypeEnum.Disease);
        rptDisease.DataSource = dtSort;
        rptDisease.DataBind();

        //广告栏
        DataTable dtAd= new BisBanner().GetListBySort(AdverTypeEnum.Disease,nSortID);
        foreach (DataRow drAd in dtAd.Rows)
        {
            strAd += (strAd == string.Empty ? "" : ",") + "['/" + (WebConfigInfo.FilePlace + "d/" + drAd["ImgPlace"].ToString()) + "', '/" + (WebConfigInfo.FilePlace + "d/" + drAd["ImgPlace"].ToString()) + "', " + (drAd["Url"].ToString() == string.Empty ? "null" : "'" + drAd["Url"].ToString() + "'") + ", \"\", \"\"]";
        }

        //DataTable dtFaq = new BisFeedBack().GetTopList(5, 0);
        //if (dtFaq.Rows.Count > 0)
        //{
        //    ltlFaqTitle.Text = Server.HtmlEncode(dtFaq.Rows[0]["Title"].ToString());
        //    strInfo = Common.NoHTML(dtFaq.Rows[0]["Info"].ToString());
        //    ltlFaqInfo.Text = (strInfo.Length > 25 ? strInfo.Substring(0, 25) : strInfo) + "…" +
        //                     "<a href=\"/faq/" + dtFaq.Rows[0]["SortUrlName"].ToString() + "/" + dtFaq.Rows[0]["ID"].ToString() + ".html\" title=\"" + dtFaq.Rows[0]["Title"].ToString() + "\" target=\"_blank\">[详情]</a>";
        //    dtFaq.Rows.RemoveAt(0);
        //    dtFaq.AcceptChanges();
        //}
        //rptFaq.DataSource = dtFaq;
        //rptFaq.DataBind();
     
    }


      /// <summary>
    /// 疾病列表绑定处理
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void rptDisease_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        nCount++;
        DataTable dtSort = (DataTable)((Repeater)sender).DataSource;
        int nSortID = Convert.ToInt32(dtSort.Rows[e.Item.ItemIndex]["ID"]);
        DataTable dtArticle = new BisArticle().GetTopArticle(5, ArticelTypeEnum.Disease, nSortID);
        string strInfo = string.Empty;
        if (dtArticle.Rows.Count > 0)
        {
            Literal ltlTitle = e.Item.FindControl("ltlTitle") as Literal;
            ltlTitle.Text = "<a href=\"" + Until.GetUrlName(dtArticle.Rows[0]["PSortUrlName"], dtArticle.Rows[0]["SortUrlName"], dtArticle.Rows[0]["ID"]) + "\" target=\"_blank\" title=\"" + dtArticle.Rows[0]["Name"].ToString() + "\">" + Server.HtmlEncode(dtArticle.Rows[0]["Name"].ToString()) + "</a>";
            strInfo = Common.NoHTML(dtArticle.Rows[0]["Info"].ToString()).Trim();
            Literal ltlInfo = e.Item.FindControl("ltlInfo") as Literal;
            ltlInfo.Text = (strInfo.Length > 20 ? strInfo.Substring(0, 20) : strInfo) + "…" +
                             "<a href=\"" + Until.GetUrlName(dtArticle.Rows[0]["PSortUrlName"], dtArticle.Rows[0]["SortUrlName"], dtArticle.Rows[0]["ID"]) + "\" title=\"" + dtArticle.Rows[0]["Name"].ToString() + "\" target=\"_blank\">[详情]</a>";
            dtArticle.Rows.RemoveAt(0);
            dtArticle.AcceptChanges();
        }
        Repeater rptDiseaseInfo = e.Item.FindControl("rptDiseaseInfo") as Repeater;
        rptDiseaseInfo.DataSource = dtArticle;
        rptDiseaseInfo.DataBind();
    }
}